# -*- coding:utf-8 -*-

import re

from .pkg_helper import PkgHelperInterface
from src.utils import runcmd
from src.core import macro
from src.utils.logger import logger

RPM_NAMES = "openEuler-release"
SPEC_NAMES = "generic-release.spec"


class PkgRelease(PkgHelperInterface):
    def __init__(self, vendor_src, version_id, version_info):
        self._spec_names = SPEC_NAMES
        self._version_id = version_id
        self._version_info = version_info
        runcmd.rpm_src_setup(vendor_src + "/" + RPM_NAMES + "*")

    def replace_vendor(self):
        logger.warning("begin replace latest_release vendor")
        vendor_verson = self._version_info.replace("-", "_")
        _spec = macro.RPM_SRC_SETUP_SPEC_PATH + self._spec_names
        runcmd.replace(_spec, "openEuler-performance", "%{product_family}-performance")
        runcmd.replace(_spec,
                       "%{generic_version}%{generic_patch_level}_%{generic_patch_level_extend}",
                       vendor_verson)
        runcmd.replace(_spec, "%{generic_patch_level}_%{generic_patch_level_extend}", "")
        runcmd.replace(_spec, " (%{generic_patch_level}-%{generic_patch_level_extend})", "")
        runcmd.replace(_spec, "generic_version 20.03", "generic_version {}".format(self._version_info))
        runcmd.replace(_spec, "Provides:",
                       "Provides:\topenEuler-release\nProvides:", 1)
        runcmd.replace(_spec, "Source0", "Provides:\tsystem-release = 20.03LTS_SP1\nSource0", 1)
        runcmd.replace(_spec, "Requires:       %{product_family}-repos",
                       "Requires:       openEuler-repos")

    def replace_rpm(self):
        pass

